package by.artureg.so.conf;

import by.artureg.so.ex.SoException;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Root
public class Report {
    @Attribute
    private String name;
    @Attribute(required = false)
    private String label;

    @Element(name = "SQL")
    private String sql;

    @ElementList(name = "Parameters", required = false)
    private List<Parameter> parameters;
    @ElementList(name = "Fields", required = false)
    private List<Field> fields;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public List<Parameter> getParameters() {
        return parameters;
    }

    public void setParameters(List<Parameter> parameters) {
        this.parameters = parameters;
    }

    public List<Field> getFields() {
        return fields;
    }

    public void setFields(List<Field> fields) {
        this.fields = fields;
    }

    public Parameter getParamByName(String name) throws SoException {
        if (!CollectionUtils.isEmpty(parameters)) {
            for (Parameter parameter : parameters) {
                if (parameter.getName().equals(name)) {
                    return parameter;
                }
            }
        }
        throw new SoException("parameter " + name + " is undefined");
    }

    public Field getFieldByName(String name) {
        if (!CollectionUtils.isEmpty(fields)) {
            for (Field field : fields) {
                if (field.getName().equals(name)) {
                    return field;
                }
            }
        }
        return null;
    }
}

